package com.ghy.web.backend;

import com.ghy.entity.EbNews;
import com.ghy.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/news")
public class NewsController {

    @Autowired
    private NewsService newsService;

    /* list---分页请求数据
     @RequestParam("page") page必须与请求参数的名字一样
             spring mvc自动将请求参数的值绑定list方法的psize参数
     ？page=2 ==spring mvc==》 psize=2

     @RequestParam(value = "page",defaultValue = "1")

     defaultValue=1 表示如果请求没有携带page参数，则psize的默认值为1
     */
    @RequestMapping("/list")
    public String list(Model model,
                       @RequestParam(value = "page",defaultValue = "1")int psize,
                       @RequestParam(value = "pageSize",defaultValue = "5") int pageSize){
        System.out.println("start service.....;psize==>"+psize);
//        1.请求service层获取新闻的分页数据
        List<EbNews> newsList = newsService.listByPage(psize,pageSize);
//        2.调用service层获取新闻表的总页数
        int totalPage = newsService.getMaxPage(pageSize);

//        2.传递model（数据）和view视图(jsp)  key-value
        model.addAttribute("newsList",newsList);
        model.addAttribute("totalPage",totalPage);
        model.addAttribute("curPage",psize);
        return "manage/news";
    }

    @RequestMapping("/to_save")
    public String toNewsAdd(){
        //  spring mvc框架=> /manage/news-add.jsp
        return "manage/news-add";
    }

    /**
     * spring mvc 会将表达值，根据表单的name，填充参数EbNews
     * 对应属性
     * @param ebNews
     * @return
     */
    @RequestMapping("/save")
    public String save(EbNews ebNews){
        System.out.println("ebNews==>"+ebNews);
        //1.调用service层的添加新闻的方法
        Integer result = newsService.save(ebNews);
        //2.根据添加返回过来的结果决定跳转网页
        if(result<1){
            //添加失败的情况
            return "manage/manage-error";
        }
        return "manage/manage-result";
    }

    @RequestMapping("/to_edit")
    public String toEdit(Model model,@RequestParam("enId") int enId){
        System.out.println("点击修改跳过来了，enId="+enId);
        //1.调用service层的方法查询点击的新闻的所有信息
        EbNews ebNews = newsService.selectOne(enId);

        //2.指定model和jsp
        model.addAttribute("ebNews",ebNews);

        return "manage/news-modify";//manage/nes-modify.jsp
    }

    @RequestMapping("/editSubmit")
    public String editSubmit(EbNews ebNews){
        System.out.println("ebNews==>"+ebNews);
        Integer result = newsService.updateNews(ebNews);
        if(result<1){
            return "manage/manage-error";
        }
        return "manage/manage-result";
    }

    @RequestMapping("/delete/{enId}")
    public String delete(@PathVariable ("enId") Integer enId){
        //1.参数校验
        //2.执行删除（真实项目中 delete from 表名 where id=？）
        int result = newsService.remove(enId);
        if(result==0){
            return "manage/manage-error";
        }
        return "manage/manage-result";
    }


}
